#include <head.h>
#include "signal.h"
#define STR1 "HELLOWORLD"

#if 0
// void signal_recycle_child_test()
int main(int argc, char **argv)
{
    pid_t p;
    if(argc!= 2){
        fprintf(stderr,"Usage:argv[%s] fifo name\n",argv[0]);
        exit(1);
    }
    // int fifo= mkfifo("myfifo",0666);
    int fifo= open(argv[1],O_RDWR);
    if(-1 == fifo){
        perror("mkfifo error");
        exit(1);
    }

    p = fork();
   
		
	
	if(p>0){
        DEBUG_MSG("parent process\n");
	    // int  r= dup2(fifo,STDIN_FILENO);
        // if(-1 == r){
        //     perror("dup2 error");
        //     exit(1);
        // }
        char c;
        int len=0;
        char buf[1024];
        
        while( (len = read(fifo,buf,sizeof(buf)-1))>0  ){
            printf("%s",buf);
        }
		
	}
	else{
        DEBUG_MSG("son process\n");
        // dup2()
		for (size_t k = 0; k < 5; k++)
        {
            write(fifo,STR1,strlen(STR1));
            sleep(1);
        }
        while (1)
        {
           
        }
        	
	}
    //close(fifo);
	return 0;
}
#endif

#if 1
int named_fifo_test_main(int argc, char **argv)
{
    pid_t p;
    if(argc!= 2){
        fprintf(stderr,"Usage:argv[%s] fifo name\n",argv[0]);
        exit(1);
    }
    // int fifo= open(argv[1],O_RDWR);
    // int fifo= mkfifo("myfifo",0666);
   
    // if(-1 == fifo){
    //     perror("open fifo error");
    //     exit(1);
    // }

    int i=0;
    for ( i = 0; i < 2; i++)
    {
        pid_t tmp = fork();
        if(tmp == 0){
            break;
        }
    }	
	
	if(i == 2){
         sleep(5);
        printf("parent:before open\n");
        int fifo= open(argv[1],O_RDONLY);
        printf("parent:after open\n");
        if(-1 == fifo){
            perror("open fifo error");
            exit(1);
        }
        DEBUG_MSG("parent process\n");
	    // int  r= dup2(fifo,STDIN_FILENO);
        // if(-1 == r){
        //     perror("dup2 error");
        //     exit(1);
        // }
        char c;
        int len=0;
        char buf[1024];
        memset(buf,0,sizeof(buf));
        // int flags;
        // if((flags = fcntl(fifo, F_GETFL, 0)) < 0)
        // {
        //     perror("fcntl");
        //     return -1;
        // }
        // flags |= O_NONBLOCK;
        // if(fcntl(fifo, F_SETFL, flags) < 0)
        // {
        //     perror("fcntl");
        //     return -1;
        // }

        while(  1){
            len = read(fifo,buf,sizeof(buf)-1);
            // printf("read len =%d\n",len);
            if(!(len>0)){
                break;
            }
            printf("%s\n",buf);
            memset(buf,0,sizeof(buf));
        }
		
	}
	else if(i == 1){
        
        DEBUG_MSG("son[%d] process\n",getpid());
        // dup2()
        printf("son[%d]:before open\n",getpid());
        int fifo= open(argv[1],O_WRONLY);
        printf("son[%d]:after open\n",getpid());
        if(-1 == fifo){
            perror("open fifo error");
            exit(1);
        }
        char buf[1024];

		for (;;)
        {
            memset(buf,0,sizeof(buf));
            snprintf(buf,sizeof(buf),"[%d] ",getpid());
            time_t timer = time(NULL);
            write(fifo,buf,strlen(buf));
            //printf("ctime is %s\n", ctime(&timer));//得到日历时间
            write(fifo,ctime(&timer),strlen(ctime(&timer))-1);
            sleep(5);
        }	
	}
    //close(fifo);
	return 0;
}
#endif